package com.sc.general.filter;


import com.alibaba.fastjson.JSON;
import com.sc.general.vo.ResultVO;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class ResponseFilter implements ResponseBodyAdvice {

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest request, ServerHttpResponse response) {
        System.out.println(request.getURI().getPath());
        if (request.getURI().getPath().indexOf("/swagger-resources")>-1 || request.getURI().getPath().indexOf("/v2")>-1){
            return o;
        }
        if(o instanceof ResultVO){
            return o;
        }
        Object re = ResultVO.success(o);
        if(o instanceof String) {
            return JSON.toJSONString(re);
        }else{
            return re;
        }
//        return o;
    }


    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }
}